package com.edu.edu.day9_19_week7_homework.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.edu.edu.day9_19_week7_homework.R;
import com.edu.edu.day9_19_week7_homework.bean.Food;

import java.util.List;

/**
 * Created by Administrator on 9/20 0020.
 */
public class FoodAdapter extends BaseAdapter{

    private List<Food> mData;
    private LayoutInflater flater;

    public void setData(List<Food> data) {
        this.mData = data;
        notifyDataSetChanged();
    }
    public FoodAdapter(Context context) {
        flater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public Object getItem(int position) {

        return mData != null ? mData.get(position) : null;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertview, ViewGroup parent) {
        ViewHolder holder;
        if(convertview ==null){
            convertview= flater.inflate(R.layout.list_item,parent,false);
            holder = new ViewHolder(convertview);
            convertview.setTag(holder);
        }else{
            holder = (ViewHolder) convertview.getTag();
        }
        //绑定数据
        Food food = mData.get(position);
        String recommend_caption = food.getRecommend_caption();
        String food_likes_count = food.getLikes_count();
        holder.recommend_caption.setText( recommend_caption);
        holder.food_likes_count.setText(food_likes_count);
        String recommend_cover_pic = food.getRecommend_cover_pic();
        //利用glide缓存图片
        Glide.with(parent.getContext()).load(recommend_cover_pic).placeholder(R.mipmap.ic_launcher).into(holder.recommend_cover_pic);
        String user_avator = food.getAvatar();
        //利用glide缓存图片
        Glide.with(parent.getContext()).load(user_avator).placeholder(R.mipmap.ic_launcher).into(holder.user_avator);
        return convertview;
    }
    class ViewHolder{
        ImageView recommend_cover_pic,user_avator;
        TextView recommend_caption,food_likes_count;
        public ViewHolder(View convertview){
            recommend_cover_pic = (ImageView) convertview.findViewById(R.id.food_recommend_cover_pic);
            user_avator = (ImageView) convertview.findViewById(R.id.user_avator);
            recommend_caption = (TextView) convertview.findViewById(R.id.recommend_caption);
            food_likes_count = (TextView) convertview.findViewById(R.id.food_likes_count);
        }
    }
}
